package com.hx.vending.service.impl;

import com.hx.vending.bean.Transfer;
import com.hx.vending.bean.User;
import com.hx.vending.dao.TransferMapper;
import com.hx.vending.dao.UserMapper;
import com.hx.vending.service.ITransferService;
import com.hx.vending.util.Msg;
import com.hx.vending.util.ToolUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/30.
 */
@Service
public class TransferServiceImpl implements ITransferService {
    @Resource
    private TransferMapper transferMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public Integer insertSelective(Transfer transfer) {
        return transferMapper.insertSelective(transfer);
    }

    @Override
    public Msg getTransferByUser(Integer userId,Integer account,Integer[] integers, String condition, Integer pageNum) {
        if(ToolUtil.isEmpty(userId) || userId == 0){
            return Msg.fail().add("va_msg","用户信息不能为空");
        }else{
            User user = new User();
            if (account == 0){
                user.setUserId(userId);
            }else {
                user.setUserId(account);
            }

            User u =  userMapper.selectByPrimaryKey(user);
            pageNum = (pageNum - 1) * 10;
            Map<String, Object> map = new HashMap<>();
            map.put("userId",userId);
            map.put("pageNum", pageNum);
            map.put("condition", condition);
            map.put("integers",integers);
            List<Transfer> transferList = transferMapper.selectTransferByUser(map);
            for(int i = 0;i < transferList.size();i++){
                if(ToolUtil.isNotEmpty(u)){
                    transferList.get(i).setTransferUserName(u.getUserName());
                }
            }
            return Msg.success().add("transferList", transferList);
        }
    }
}
